#include "Common.h"
#include "OctreeNode.h"
#include "OctreeScene.h"
#include "Scene.h"


OctreeNode::OctreeNode(const std::string& name, Scene* owner)
	: SceneNode(name, owner)
{
}

OctreeNode::~OctreeNode(void)
{
	RemoveFromScene();
}

void OctreeNode::setVisible(bool v)
{
	SceneNode::setVisible(v);
}

void OctreeNode::UpdateImpl()
{
	SceneNode::UpdateImpl();

	RemoveFromScene();
	AddToScene();
}

void OctreeNode::AddToScene()
{
	if (!mOctreeId.IsValidId())
	{
		SceneOctree* sceneOctree = getOwner()->getSceneOctree();

		OctreeNodeInfoCompact nodeInfo(this);

		sceneOctree->AddElement(nodeInfo);
		assert(mOctreeId.IsValidId());
	}
}

void OctreeNode::RemoveFromScene()
{
	if (mOctreeId.IsValidId())
	{
		SceneOctree* sceneOctree = getOwner()->getSceneOctree();

		assert(mOctreeId.IsValidId());
		sceneOctree->RemoveElement(mOctreeId);
		mOctreeId = FOctreeElementId();
	}
}